package com.atguigu.serviceedu.controller;


import com.aiguigu.common_utils.R;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.entity.chapter.CoursePublishVo;
import com.atguigu.serviceedu.entity.vo.CourseInfoVo;
import com.atguigu.serviceedu.entity.vo.CourseQuery;
import com.atguigu.serviceedu.entity.vo.TeacherQuery;
import com.atguigu.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zzc
 * @since 2022-09-16
 */
@RestController
@RequestMapping("/serviceedu/course")
//@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;
    //课程列表
    @GetMapping
    public R getCourseList(){
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);
    }

    @DeleteMapping("{id}")
    @ApiOperation("逻辑删除课程")
    public R removeCourse(@PathVariable String id){
        Boolean flag=courseService.removeCourseById(id);
        if(flag){
            return R.ok();
        }else {
            return R.error().message("删除失败,此课程已销售");
        }

    }
    @PostMapping("pageCourseCondition/{current}/{limit}")
    @ApiOperation("分页模糊查询")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) CourseQuery courseQuery){
        //@RequestBody   前端使用json传递数据，把json数据封装到对应对象里面  要使用post方法
        //@ResponseBody  返回数据 返回json数据
        Page<EduCourse> pageCourse=new Page<>(current,limit);
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();

        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }

        if (!StringUtils.isEmpty(teacherId) ) {
            wrapper.eq("teacher_id", teacherId);
        }

        if (!StringUtils.isEmpty(subjectParentId)) {
            wrapper.ge("subject_parent_id", subjectParentId);
        }

        if (!StringUtils.isEmpty(subjectId)) {
            wrapper.ge("subject_id", subjectId);
        }

        courseService.page(pageCourse,wrapper);
        long total=pageCourse.getTotal();
        List<EduCourse> records=pageCourse.getRecords();

        return R.ok().data("total",total).data("rows",records);
    }
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        //返回添加之后的课程id，为了后面添加使用
        String id = courseService.saveCourseInfo(courseInfoVo);

        return R.ok().data("courseId",id);
    }
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo =courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }
    //根据id 获得课程发确认布信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublish = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublish);
    }
    //课程最终发布 修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }
}

